#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Helper script for easy access to dragonfly simulation tools.
This script provides a convenient interface to run various tools and utilities.
"""

import sys
import os
import subprocess
import argparse

def run_main_simulation():
    """Run the main simulation."""
    print("Running main simulation...")
    subprocess.run([sys.executable, "main.py"])

def run_short_test():
    """Run a short test simulation."""
    print("Running short test simulation...")
    subprocess.run([sys.executable, "tools/run_scripts/run_short_test.py"])

def run_limited_steps(steps=50):
    """Run simulation with limited steps."""
    print(f"Running simulation with {steps} steps...")
    subprocess.run([sys.executable, "tools/run_scripts/run_limited_steps.py", "--steps", str(steps)])

def run_mesh_tests():
    """Run mesh loading and basic tests."""
    print("Running mesh tests...")
    subprocess.run([sys.executable, "mesh/test_fixes.py"])

def run_solver_tests():
    """Run solver convergence tests."""
    print("Running solver tests...")
    subprocess.run([sys.executable, "tests/test_solver_fix.py"])

def view_mesh():
    """View the mesh structure."""
    print("Opening mesh viewer...")
    subprocess.run([sys.executable, "tools/visualization/view_raw_mesh.py"])

def interactive_mesh():
    """Open interactive mesh visualization."""
    print("Opening interactive mesh visualization...")
    subprocess.run([sys.executable, "tools/visualization/visualize_mesh_structure.py"])

def create_video():
    """Create video from simulation results."""
    print("Creating video from simulation results...")
    subprocess.run([sys.executable, "tools/video/create_video.py", 
                   "--input", "tools/run_scripts/output/visualization",
                   "--fps", "5"])

def create_gif():
    """Create GIF animation from simulation results."""
    print("Creating GIF animation from simulation results...")
    # Change to tools/video directory and run the GIF script
    import os
    original_dir = os.getcwd()
    try:
        os.chdir("tools/video")
        subprocess.run(["bash", "create_gif.sh", "-i", "../run_scripts/output/visualization", 
                       "-o", "dragonfly_simulation.gif", "-f", "2", "-s", "600"])
    finally:
        os.chdir(original_dir)

def generate_mesh():
    """Generate or regenerate the mesh."""
    print("Generating mesh...")
    subprocess.run([sys.executable, "mesh/generate_mesh.py"])

def main():
    parser = argparse.ArgumentParser(description='Dragonfly Simulation Tools')
    parser.add_argument('command', nargs='?', default='help', 
                       choices=['simulate', 'test', 'test-mesh', 'test-solver', 'view-mesh', 
                               'interactive-mesh', 'video', 'gif', 'generate-mesh', 'limited', 'help'],
                       help='Tool to run')
    parser.add_argument('--steps', type=int, default=50, 
                       help='Number of steps for limited simulation (default: 50)')
    
    args = parser.parse_args()
    
    if args.command == 'help':
        print("Dragonfly Simulation Tools")
        print("=" * 50)
        print("Available commands:")
        print("  simulate      - Run the full simulation")
        print("  test          - Run short test simulation")
        print("  limited       - Run limited steps simulation (use --steps N)")
        print("  test-mesh     - Test mesh loading and basic functionality")
        print("  test-solver   - Test solver convergence")
        print("  view-mesh     - View raw mesh structure")
        print("  interactive-mesh - Interactive mesh visualization")
        print("  video         - Create video from simulation results")
        print("  gif           - Create GIF animation from simulation results")
        print("  generate-mesh - Generate/regenerate mesh")
        print("  help          - Show this help message")
        print()
        print("Examples:")
        print("  python run_tools.py simulate")
        print("  python run_tools.py test")
        print("  python run_tools.py limited --steps 100")
        print("  python run_tools.py view-mesh")
        
    elif args.command == 'simulate':
        run_main_simulation()
    elif args.command == 'test':
        run_short_test()
    elif args.command == 'limited':
        run_limited_steps(args.steps)
    elif args.command == 'test-mesh':
        run_mesh_tests()
    elif args.command == 'test-solver':
        run_solver_tests()
    elif args.command == 'view-mesh':
        view_mesh()
    elif args.command == 'interactive-mesh':
        interactive_mesh()
    elif args.command == 'video':
        create_video()
    elif args.command == 'gif':
        create_gif()
    elif args.command == 'generate-mesh':
        generate_mesh()

if __name__ == "__main__":
    main() 